package com.ncmmall.application.business.live;

import com.feijin.commons.lang.Requires;
import com.ncmmall.domain.business.live.LiveSetting;
import com.ncmmall.domain.business.live.LiveSettingRepository;
import com.ncmmall.querychannel.service.QueryChannelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Collections;

@Service
@Transactional
public class LiveSettingApplication {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private LiveSettingRepository settingRepository;

    /**
     * 直播间开通协议
     */
    public void modifyProtocol(String protocol) {
        LiveSetting old = findLastSetting();
        LiveSetting setting = new LiveSetting();
        if (old != null) {
            BeanUtils.copyProperties(old, setting);
        }
        setting.setId(null);
        setting.setOpenProtocol(protocol);
        setting.setCreateTime(new Timestamp(System.currentTimeMillis()));
        settingRepository.save(setting);
    }

    /**
     * 直播关于我们
     */
    public void aboutUs(String aboutUs) {
        LiveSetting old = findLastSetting();
        LiveSetting setting = new LiveSetting();
        if (old != null) {
            BeanUtils.copyProperties(old, setting);
        }
        setting.setId(null);
        setting.setAboutUs(aboutUs);
        setting.setCreateTime(new Timestamp(System.currentTimeMillis()));
        settingRepository.save(setting);
    }

    public LiveSetting findLastSetting() {
        return queryChannelService.queryOneEntity("select * from live_setting order by createTime desc limit 1", Collections.emptyMap(), LiveSetting.class);
    }

    /**
     * 结算设置
     *
     * @param params 设置
     */
    public void modify(LiveSetting params) {

        Requires.isTrue(params.getMinPrice() != null && params.getMinPrice() >= 0, "请填写正确的单笔提现最小金额");
        Requires.isTrue(params.getMaxPrice() != null && params.getMaxPrice() >= params.getMinPrice(), "请填写正确的单笔提现最大金额");
        Requires.isTrue(params.getWithdrawNumPerDay() != null && params.getWithdrawNumPerDay() >= 0, "请填写正确的每天可提现次数");
        Requires.isTrue(params.getWithdrawNumPerMonth() != null && params.getWithdrawNumPerMonth() >= params.getWithdrawNumPerDay(), "请填写正确的每月可提现次数");
        Requires.isTrue(params.getDistributionDuration() != null && params.getDistributionDuration() >= 0, "请填写正确的佣金分成时间");
        Requires.isTrue(params.getCommissionPayType() != null, "请选择佣金支付方式");

        LiveSetting old = findLastSetting();
        LiveSetting setting = new LiveSetting();
        if (old != null) {
            BeanUtils.copyProperties(old, setting);
        }

        setting.setId(null)
                .setCreateTime(new Timestamp(System.currentTimeMillis()))
                .setMinPrice(params.getMinPrice())
                .setMaxPrice(params.getMaxPrice())
                .setWithdrawNumPerDay(params.getWithdrawNumPerDay())
                .setWithdrawNumPerMonth(params.getWithdrawNumPerMonth())
                .setDistributionDuration(params.getDistributionDuration())
                .setCommissionPayType(params.getCommissionPayType());

        settingRepository.save(setting);

    }
}
